package com.ruoyi.commodity.controller;

import com.ruoyi.commodity.domain.Commodity;
import com.ruoyi.commodity.domain.vo.CommodityQueryVo;
import com.ruoyi.commodity.domain.vo.CommodityVo;
import com.ruoyi.commodity.service.ICommodityService;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;

import com.ruoyi.common.security.utils.SecurityUtils;

import lombok.extern.log4j.Log4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/commodity")
public class CommodityController extends BaseController {


    @Autowired
    private ICommodityService commodityService;
    private String commodityId;


    @GetMapping("/list")
    public TableDataInfo list(CommodityQueryVo commodityQueryVo) {
        List<CommodityVo> list = commodityService.selectCommodityList(commodityQueryVo);
        return getDataTable(list);
    }

    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addCommodity(@Validated @RequestBody Commodity commodity) {
        if (!commodityService.checkCommodityNameUnique(commodity)) {
            return error("新增商品'" + commodity.getName() + "'失败，商品名称已存在");
        }
        commodity.setCreateBy(SecurityUtils.getUsername());
        return toAjax(commodityService.insertCommodity(commodity));
    }

    @DeleteMapping("/{commodityId}")
    public AjaxResult deleteCommodity(@PathVariable("commodityId") Long commodityId) {
        int rows = commodityService.deleteCommodityById(commodityId);
        return rows > 0 ? AjaxResult.success("删除商品成功") : AjaxResult.error("删除商品失败");
    }

    @PutMapping
    @Transactional
    public AjaxResult updateCommodity(@RequestBody Commodity commodity) {
        try {
            boolean isUnique = commodityService.checkCommodityNameUnique(commodity);
            if (!isUnique) {
                return AjaxResult.error("修改商品失败，商品名称已存在");
            }

            int rows = commodityService.updateCommodity(commodity);
            if (rows < 1) {
                return AjaxResult.error("修改商品失败");
            }

            return AjaxResult.success();
        } catch (Exception e) {
            // 发生异常时进行回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("修改商品失败，发生异常：" + e.getMessage());
        }
    }

    @GetMapping("/{commodityId}")
    public CommodityVo getCommodityById(@PathVariable String commodityId) {
        this.commodityId = commodityId;
        System.out.println("@@@@@@@@@@@@@@@@@"+commodityId);
        return commodityService.selectCommodityById(Long.parseLong(commodityId));
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response,CommodityQueryVo commodityQueryVo) {
        List<CommodityVo> list = commodityService.selectCommodityList(commodityQueryVo);
        if (StringUtils.isNotEmpty(list)) {
            ExcelUtil<CommodityVo> util = new ExcelUtil<>(CommodityVo.class);
            util.exportExcel(response, list, "商品数据");
        }
    }
}




